#include "stdio.h"
#include "stdlib.h"

#define status int
#define OK 1
#define ERROR 0
#define OVERFLOW -1
// 队列

typedef int QElemType;
typedef struct QNode
{
	QElemType data;
	struct QNode *next;
}QNode,*QueuePtr;

typedef struct 
{
	QueuePtr front;
	QueuePtr rear;
}LinkQueue;

status InitQueue(LinkQueue &Q);
status EnQueue(LinkQueue &Q,QElemType e);
status DeQueue(LinkQueue &Q,QElemType &e);
status showQueue(LinkQueue Q);


status InitQueue(LinkQueue &Q){
	Q.front = (QueuePtr)malloc(sizeof(QNode));
	if(!Q.front) exit(OVERFLOW);
	Q.rear = Q.front;
	Q.front->next = NULL;
}

status EnQueue(LinkQueue &Q,QElemType e){
	QueuePtr p;
	p = (QueuePtr)malloc(sizeof(QNode));
	if(!p) exit(OVERFLOW);
	p->next = NULL;
	p->data = e;
	Q.rear->next = p;
	Q.rear = p;
	return OK;
}


status DeQueue(LinkQueue &Q,QElemType &e){
	QueuePtr S;
	if(!Q.front->next) return ERROR;
	S = Q.front->next;
	e = S->data;
	Q.front->next = S->next;
	if(!Q.front->next) Q.rear=Q.front;
	free(S);
	return OK;
}


status showQueue(LinkQueue Q){
	Q.front = Q.front->next;
	while(Q.front){
		printf("%d\n", Q.front->data);
		Q.front = Q.front->next;
	}
}